#! /bin/sh -
#
# Simple "configure" script for Qi.
#
# Copyright (c) 2016-2018, 2020-2021 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

usage() {
    printf '%s' \
"Usage: configure [options]

Defaults for the options are specified in brackets.

Options:
  --prefix=DIR         install files in DIR [${prefix}]
  --exec-prefix=DIR    base DIR for arch-dependent files [${exec_prefix}]
  --bindir=DIR         user executables [${bindir}]
  --sbindir=DIR        system admin executables [${sbindir}]
  --libexecdir=DIR     program executables [${libexecdir}]
  --sysconfdir=DIR     read-only single-machine data [${sysconfdir}]
  --localstatedir=DIR  modifiable single-machine data [${localstatedir}]
  --datarootdir=DIR    read-only arch-independent data root [${datarootdir}]
  --infodir=DIR        info documentation [${infodir}]
  --mandir=DIR         man documentation [${mandir}]
  --docdir=DIR         documentation root [${docdir}]
  --packagedir=DIR     set directory for package installations [${packagedir}]
  --targetdir=DIR      set target directory for symbolic links [${targetdir}]
  --arch=name          set default package architecture [${arch}]

"
}

# Defaults
prefix=/usr/local
exec_prefix='$(prefix)'
bindir='$(exec_prefix)/bin'
sbindir='$(exec_prefix)/sbin'
libexecdir='$(exec_prefix)/libexec'
sysconfdir='$(prefix)/etc'
localstatedir='$(prefix)/var'
datarootdir='$(prefix)/share'
infodir='$(datarootdir)/info'
mandir='$(datarootdir)/man'
docdir='$(datarootdir)/doc'
packagedir='/usr/local/pkgs'
targetdir='/usr/local'
arch="$(uname -m)"

return_variables() {
    printf '%s\n' \
     "prefix = $prefix"                \
     "exec_prefix = $exec_prefix"      \
     "bindir = $bindir"                \
     "sbindir = $sbindir"              \
     "libexecdir = $libexecdir"        \
     "sysconfdir = $sysconfdir"        \
     "localstatedir = $localstatedir"  \
     "datarootdir = $datarootdir"      \
     "infodir = $infodir"              \
     "mandir = $mandir"                \
     "docdir = $docdir"                \
     "packagedir = $packagedir"        \
     "targetdir = $targetdir"          \
     "arch = $arch"                    \
     ""
}

# Handle options
while test $# -gt 0
do
    case $1 in
    --prefix)
        prefix="$2"
        shift
        ;;
    --prefix=*)
        prefix="${1#*=}"
        ;;
    --exec-prefix)
        exec_prefix="$2"
        shift
        ;;
    --exec-prefix=*)
        exec_prefix="${1#*=}"
        ;;
    --bindir)
        bindir="$2"
        shift
        ;;
    --bindir=*)
        bindir="${1#*=}"
        ;;
    --sbindir)
        sbindir="$2"
        shift
        ;;
    --sbindir=*)
        sbindir="${1#*=}"
        ;;
    --libexecdir)
        libexecdir="$2"
        shift
        ;;
    --libexecdir=*)
        libexecdir="${1#*=}"
        ;;
    --sysconfdir)
        sysconfdir="$2"
        shift
        ;;
    --sysconfdir=*)
        sysconfdir="${1#*=}"
        ;;
    --localstatedir)
        localstatedir="$2"
        shift
        ;;
    --localstatedir=*)
        localstatedir="${1#*=}"
        ;;
    --datarootdir)
        datarootdir="$2"
        shift
        ;;
    --datarootdir=*)
        datarootdir="${1#*=}"
        ;;
    --infodir)
        infodir="$2"
        shift
        ;;
    --infodir=*)
        infodir="${1#*=}"
        ;;
    --mandir)
        mandir="$2"
        shift
        ;;
    --mandir=*)
        mandir="${1#*=}"
        ;;
    --docdir)
        docdir="$2"
        shift
        ;;
    --docdir=*)
        docdir="${1#*=}"
        ;;
    --packagedir)
        packagedir="$2"
        shift
        ;;
    --packagedir=*)
        packagedir="${1#*=}"
        ;;
    --targetdir)
        targetdir="$2"
        shift
        ;;
    --targetdir=*)
        targetdir="${1#*=}"
        ;;
    --arch)
        arch="$2"
        shift
        ;;
    --arch=*)
        arch="${1#*=}"
        ;;
    --help | --hel | --he | --h | '--?' | -help | -hel | -he | -h | '-?' )
        usage
        exit
        ;;
    --)
        shift
        break;	# End of options.
        ;;
    -*)
        echo "configure: WARNING: unrecognized option '${1}'" 1>&2
        ;;
    *)
        break
        ;;
    esac
    shift
done

echo "Creating config.mak ..."

return_variables;		# Show configured variables.
: > config.mak;			# Clean up config.mak, first.

if return_variables > config.mak
then
    touch src/qi.in && echo "OK, now you can run \`make'"
fi

